package com.baizhi.controller;

import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Video;
import com.baizhi.service.VideoService;
import com.baizhi.vo.CommonQueryPageVO;
import com.baizhi.vo.CommonVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("video")
public class VideoController {

        private static final Logger log = LoggerFactory.getLogger(VideoController.class);
        @Resource
        VideoService videoService;

        @ResponseBody
        @RequestMapping("queryAllPage")
        public CommonQueryPageVO queryAllPage(@RequestBody PageDTO pageDTO) {

            log.info("当前页：{}", pageDTO.getPage());
            log.info("每页展示条数：{}", pageDTO.getPageSize());

            CommonQueryPageVO commonQueryPageVO = videoService.queryAllPages(pageDTO.getPage(), pageDTO.getPageSize());

            return commonQueryPageVO;
        }
    @ResponseBody
    @RequestMapping("update")
    public CommonVO update(@RequestBody Video video) {
        log.info("修改用户数据------：{}", video);

        try {
            videoService.update(video);
            return CommonVO.success("修改成功！！！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            System.out.println("---" + e.getMessage());
            return CommonVO.faild("修改失败！！！");
        }
    }
    @ResponseBody
    @RequestMapping("add")
    public CommonVO add(@RequestBody Video video) {
        log.info("添加用户数据------：{}", video);
        try {
            videoService.add(video);
            return CommonVO.success("添加成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.faild("添加失败！！！");
        }
    }
    @ResponseBody
    @RequestMapping("uploadHeadImg")
    public HashMap<String, String> uploadHeadImg(MultipartFile videoFile) {

        return videoService.uploadHeadImg(videoFile);

    }

    @ResponseBody
    @RequestMapping("delete")
    public CommonVO delete(@RequestBody Video video) {
        log.info("修改用户数据------：{}", video);

        try {
            videoService.delete(video);
            return CommonVO.success("删除成功！！！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            System.out.println("---" + e.getMessage());
            return CommonVO.faild("删除失败！！！");
        }
    }
    @RequestMapping("queryById")
    public Video queryById(String id) {
        log.info("查询管理员id：{}", id);
        return videoService.queryById(id);
    }

    @ResponseBody
    @RequestMapping("searchVideo")
    public List<Video> searchVideo(String content){

        System.out.println(content);

        List<Video> videos = null;
        try {
            videos = videoService.searchVideo(content);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return videos;
    }
}
